<template>
  <div class="box">
    <el-button type="primary" @click="dialogVisible = true">新增</el-button>
    <el-dialog
      title="新增"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="项目名称" prop="entryName">
        <el-input v-model="ruleForm.entryName"></el-input></el-form-item>
        <el-form-item label="项目地址" prop="ProjectAddress">
          <el-input v-model="ruleForm.ProjectAddress"></el-input></el-form-item>
            <el-form-item label="项目简介" prop="ProjectBief">
          <el-input v-model="ruleForm.ProjectBief"></el-input>
        </el-form-item>
        <el-form-item label="项目状态" prop="ProjectStatus">
          <el-select v-model="ruleFor0m.ProjectStatus" placeholder="请选择活动区域">
            <el-option label="有效" value="有效"></el-option>
            <el-option label="无效" value="无效"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即新增</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-table v-loading='loading' border="" :data="tableData" stripe style="width: 100%">
      <el-table-column prop="SerialNumber" label="序号" width="180">
        <template scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="project_name" label="项目名称" width="180">
      </el-table-column>
      <el-table-column prop="project_site" label="项目地址" width="280">
      </el-table-column>
      <el-table-column prop="project_sketch" label="项目简介" width="180">
      </el-table-column>
      <el-table-column prop="project_state" label="项目状态"></el-table-column>
      <el-table-column prop="operation" label="操作">
        <button>编辑</button>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading:true,
      tableData: [],
      dialogVisible: false,
      ruleForm: {
        entryName: "",
        ProjectAddress: "",
        ProjectBief: "",
        ProjectStatus:''
      },
      rules: {
        entryName: [
          { required: true, message: "请输入项目名称", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        ProjectAddress: [
          { required: true, message: "请输入项目地址", trigger: "blur" },
          { trigger: "blur" },
        ],
        ProjectBief: [
          { required: true, message: "请输入项目简介", trigger: "blur" },
          { trigger: "blur" },
        ],
        ProjectStatus: [
          { required: true, message: "请选择项目状态", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.aa();
  },
  methods: {
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done();
        })
        .catch(() => {});
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http.post('/api/project',{
            project_name:this.ruleForm.entryName,
            project_site:this.ruleForm.ProjectAddress,
            project_sketch:this.ruleForm.ProjectBief,
            project_state:this.ruleForm.ProjectStatus,
          }).then(()=>{
            this.aa()
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    aa() {
      this.$http.get("/api/project").then((res) => {
        console.log(res.data);
        this.loading=false
        if(res.data.code==2000){
          this.tableData = res.data.project;
        }else{
          this.$message.error(res.data.msg);
          localStorage.removeItem('access_token')
        }
      }).catch((err)=>{
        console.log(err.message);
        this.loading=false
        this.$message.error(err.message);
      });
    },
  },
};
</script>